﻿using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using CodeCamp.BL;
using CodeCamp.BL.Helpers;
using CodeCamp.BL.Model;
using CodeCamp.BL.UI;

namespace CodeCamp.Web.Controllers
{

	public class SpeakerController : CodeCampController
	{

		private static readonly string[] ALLOWED_IMAGE_TYPES = new[] { "image/jpeg", "image/jpg", "image/gif", "image/png", "image/pjpeg" };

		internal const int MAX_IMAGE_SIZE = 10000;
		[MvcMenuItem("My Profile", Order = 0, ParentLink = "/Speaker/List")]
		[Authorize]
		public ActionResult Index()
		{
			var p = CodeCampBL.GetMemberProfile(GetUserID());
			if (p == null)
			{
				p = new MemberProfile();
			}
			return View(p);
		}

		[MvcMenuItem("Edit My Profile", Order = 10, ParentLink = "/Speaker/List")]
		[Authorize]
		public ActionResult Edit()
		{
			return Index();
		}

		[Authorize]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Edit(MemberProfile p, HttpPostedFileBase presenterImage)
		{
			if (ModelState.IsValid)
			{
				p.UserId = GetUserID();
				CodeCampBL.SaveMemberProfile(p);
				if (presenterImage != null)
				{
					if (presenterImage.IsValidUploadedFile(ALLOWED_IMAGE_TYPES, MAX_IMAGE_SIZE))
					{
						CodeCampBL.SaveMemberPhoto(presenterImage, p.UserId);
					}
					else
					{
						this.ShowError("Image format not allowed:" + presenterImage.ContentType);
						return View(p);
					}
				}
				this.ShowMessage("Your profile has been saved!");
				return RedirectToAction("Index");
			}
			
			ModelState.AddModelError("error", "Please correct errors and resubmit");
			
			return View(p);
		}

		[MvcMenuItem("Speakers", Order = 8)]
		public ActionResult List()
		{
			List<MemberProfile> speakers = CodeCampBL.ListSpeakers();
			return View(speakers);
		}
	}
}